package com.example.callcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.callcenter.entity.SysUser;
import com.example.callcenter.mapper.SysUserMapper;
import com.example.callcenter.service.SysUserService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Override
    public SysUser getByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<SysUser>()
                .eq("username", username));
    }

    /**
     * Spring Security认证接口实现
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = getByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        // 返回Spring Security用户对象
        return new User(
                user.getUsername(),
                user.getPassword(),
                Collections.emptyList() // 权限列表（实际项目需根据角色配置）
        );
    }
}